/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.util.List;
import javax.persistence.EntityManager;
import model.Category;

/**
 *
 * @author hendro
 */
public class CategoryDAO {
    private EntityManager em;
    public CategoryDAO(){}

    public CategoryDAO(EntityManager em) {
        this.em = em;
    }
    public void save(Category cat) {
        if (cat.getId() == null) {
            em.persist(cat);
        } else {
            em.merge(cat);
        }
    }

    public void delete(Category cat) {
        em.remove(cat);
    }

    public Category getCategory(Long id) {
        return em.find(Category.class, id);
    }

    public List<Category> getCategories() {
        return em.createQuery("select p from Category p").getResultList();
    }

}
